package servlet;

import net.sf.json.JSONObject;
import pojo.Admin;
import service.AdminService;
import service.impl.AdminServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * 后台登录的servlet
 */
@WebServlet(value = "/BgLoginServlet")
public class BgLoginServlet extends HttpServlet {
   @Override
   protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
       String account=request.getParameter("adAccount");
       String password=request.getParameter("adPassword");
       //调用登录方法
       AdminService as=new AdminServiceImpl();
       Admin admin=as.login(account,password);
       JSONObject json=new JSONObject();
       if(admin.getAdId()==0){
           json.put("ret",false);
       }else {
           request.getSession().setAttribute("session",admin.getAdId());
           json.put("id",admin.getAdId());
           json.put("ret",true);
       }
       PrintWriter out=response.getWriter();
       out.print(json);
   }
   
   @Override
   protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 
       doGet(request, response);
   }
}
